package com.example.huadu_server.admin.controller;

import com.example.huadu_server.admin.dto.AdminLoginDTO;
import com.example.huadu_server.admin.service.AdminAuthService;
import com.example.huadu_server.config.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/admin/auth")
public class AdminAuthController {

    @Autowired
    private AdminAuthService adminAuthService;

    /**
     * 获取登录验证码
     */
    @GetMapping("/login/code")
    public void getLoginCode(HttpServletResponse response) {
        adminAuthService.generateLoginCode(response);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public ResponseEntity<Response<Map<String, Object>>> login(@RequestBody @Valid AdminLoginDTO adminLoginDTO) {
        Response<Map<String, Object>> response = adminAuthService.login(
                adminLoginDTO.getEmail(),
                adminLoginDTO.getPassword(),
                adminLoginDTO.getCode()
        );
        return ResponseEntity.status(response.getCode()).body(response);
    }
}
